package com.rr.sc.model;

import java.io.Serializable;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

@Entity
@Table(name = "categories", uniqueConstraints = { @UniqueConstraint(name = "unique_name_per_user", columnNames = { "user_id", "name" }) })
@SequenceGenerator(name = "categories_seq", sequenceName = "categories_seq")
public class Category implements Serializable {

    private static final long serialVersionUID = -584731402949500067L;

    private Long id;
    private User user;
    private String name;
    private String description;
    private Category parentCategory;
    private List<Category> subCategories;

    @Id
    @GeneratedValue(generator = "categories_seq", strategy = GenerationType.AUTO)
    public Long getId() {
	return id;
    }

    public void setId(Long id) {
	this.id = id;
    }

    /**
     * @return the user
     */
    @ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE })
    @JoinColumn(name = "user_id")
    public User getUser() {
	return user;
    }

    /**
     * @param user
     *            the user to set
     */
    public void setUser(User user) {
	this.user = user;
    }

    /**
     * @return the name
     */
    @Column(name = "name")
    public String getName() {
	return name;
    }

    /**
     * @param name
     *            the name to set
     */
    public void setName(String name) {
	this.name = name;
    }

    /**
     * @return the description
     */
    public String getDescription() {
	return description;
    }

    /**
     * @param description
     *            the description to set
     */
    public void setDescription(String description) {
	this.description = description;
    }

    /**
     * @return the parentCategory
     */
    @ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE })
    @JoinColumn(name = "parent_category_id")
    public Category getParentCategory() {
	return parentCategory;
    }

    /**
     * @param parentCategory
     *            the parentCategory to set
     */
    public void setParentCategory(Category parentCategory) {
	this.parentCategory = parentCategory;
    }

    /**
     * @return the subCategories
     */
    @OneToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, mappedBy = "parentCategory")
    public List<Category> getSubCategories() {
	return subCategories;
    }

    /**
     * @param subCategories
     *            the subCategories to set
     */
    public void setSubCategories(List<Category> subCategories) {
	this.subCategories = subCategories;
    }

    @Override
    public String toString() {
	StringBuilder builder = new StringBuilder();
	builder.append(super.toString() +":Category[").append(id).append(", ").append(user).append(", ").append(name).append(", ").append(description)
		.append(", p=").append(parentCategory).append("]");
	return builder.toString();
    }
}
